//
//  OrderSubListViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//
#import "OrderDetailViewController.h"
#import "OrderSubListViewController.h"

#import "OrderListCell.h"
#import "OrderSectionHeader.h"
#import "OrderSectionFooter.h"

@interface OrderSubListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,   copy) NSString *jumpType;
@property (assign, nonatomic) ETTOrderListType type;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <OrderListModel *> *dataArray;

@end

@implementation OrderSubListViewController

@synthesize pageNum;
@synthesize pageSize;

- (instancetype)initWithType:(ETTOrderListType)type {
    self = [super init];
    
    if (self) {
        _type = type;
        
        switch (type) {
            case ETTOrderListType_All:
            {
                self.jumpType = @"";
            } break;
            case ETTOrderListType_UnPay:
            {
                self.jumpType = @"1";
                
            } break;
            case ETTOrderListType_UnSend:
            {
                self.jumpType = @"2";
                
            } break;
            case ETTOrderListType_UnReceive:
            {
                self.jumpType = @"3";
                
            } break;
            case ETTOrderListType_Finish:
            {
                self.jumpType = @"9";
                
            } break;
            case ETTOrderListType_Invalid:
            {
                self.jumpType = @"9";
                
            } break;
                
            default:
                break;
        }
    }
    
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"orderRefreshNoti" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initParam];
    [self initView];
    [self request_OrderListData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orderRefreshAction:) name:@"orderRefreshNoti" object:nil];
}

-(void)orderRefreshAction:(NSNotification *)Noti {

    [self initParam];
    [self request_OrderListData];
}

- (void)initParam {
    self.pageNum = 1;
    self.pageSize = 10;
}

#pragma mark REQUEST ------------------ ORDER LIST ------------------------
- (void)request_OrderListData {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:@(self.pageNum) forKey:@"curPage"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    
    if (self.jumpType.length) {
        [dic setValue:self.jumpType forKey:@"status"];
    }

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_ORDER_LIST];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                NSArray *resultArray = [data valueForKey:@"data"];
                if ([resultArray isKindOfClass:[NSArray class]]) {
                    
                    if (self.pageNum == 1) {
                        [self.dataArray removeAllObjects];
                    }
                    
                    if (resultArray.count < self.pageSize) {
                        
                        [self.tableview.mj_footer endRefreshingWithNoMoreData];
                        
                    }
                    
                    if (self.pageNum == 1 && resultArray.count == 0) {
                        
                        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"刷新试试" didPressButton:^{
                            
                            [self request_OrderListData];
                        }];
                    }
                    
                    for (int i = 0; i < resultArray.count; i++) {
                        
                        NSDictionary *orderDic = [resultArray objectAtIndex:i];
                        OrderListModel *model = [[OrderListModel alloc] init];
                        model.supplyName = [orderDic valueForKey:@"supplierName"];
                        model.orderID = [orderDic valueForKey:@"id"];

                        NSInteger status = [[orderDic valueForKey:@"status"] integerValue];
                        switch (status) {
                            case ETTOrderDetailType_UnPay:{
                                model.orderStatus = @"待付款";
                                
                            } break;
                            case ETTOrderDetailType_UnSend:{
                                model.orderStatus = @"待发货";

                            } break;
                            case ETTOrderDetailType_UnReceive:{
                                model.orderStatus = @"待收货";

                            } break;
                            case ETTOrderDetailType_Finish:{
                                model.orderStatus = @"已完成";

                            } break;
                            case ETTOrderDetailType_Invalid:{
                                model.orderStatus = @"已失效";

                            } break;
                            default:{
                                model.orderStatus = @"";

                            } break;
                        }
                        
                        model.itemGoodsImage = [orderDic valueForKey:@"goodsImg"];
                        model.itemGoodsName = [orderDic valueForKey:@"goodsName"];
                        model.itemGoodsFreight = [[orderDic valueForKey:@"freight"] doubleValue];
                        model.itemGoodsDealPrice = [[orderDic valueForKey:@"price"] doubleValue];
                        
                        double total = [[orderDic valueForKey:@"freight"] doubleValue] + [[orderDic valueForKey:@"price"] doubleValue];
                        model.itemGoodsTotalPrice = total;

                        [self.dataArray addObject:model];
                    }
                    

                    
                    [self.tableview reloadData];
                    
                }
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
        } else {
            
        }

        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderListCellID" forIndexPath:indexPath];
    
    [cell fillCellWithModel:self.dataArray[indexPath.section]];
    
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    OrderSectionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderSectionHeaderID"];
    [header fillHeaderWithModel:self.dataArray[section]];
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    OrderSectionFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderSectionFooterID"];
    [footer fillFooterWithModel:self.dataArray[section]];
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailViewController *VC = [[OrderDetailViewController alloc] init];
    VC.orderID = self.dataArray[indexPath.section].orderID;
    [self.navigationController pushViewController:VC animated:YES];
}

- (NSMutableArray<OrderListModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}


- (void)initView {

    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"OrderListCell" bundle:nil] forCellReuseIdentifier:@"OrderListCellID"];
    
    [self.tableview registerClass:[OrderSectionHeader class] forHeaderFooterViewReuseIdentifier:@"OrderSectionHeaderID"];
    [self.tableview registerClass:[OrderSectionFooter class] forHeaderFooterViewReuseIdentifier:@"OrderSectionFooterID"];
    self.tableview.backgroundColor = PP_Default_Color_Gray_F1F1F1;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    WEAKSELF
    self.tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [weakSelf initParam];
        [weakSelf request_OrderListData];
    }];
    
    self.tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.pageNum ++;
        [weakSelf request_OrderListData];
    }];
}
@end
